package com.wave.service;

import com.alibaba.fastjson.JSONObject;
import com.wave.entity.Info;
import com.wave.mapper.InfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InfoService {

    @Autowired
    private InfoMapper infoMapper;

    public int createInfo (Info info) {
        infoMapper.createInfo(info);
        return infoMapper.createInfoData(info);
    }

    public int alterInfo (Info info){
        if (info.getInfoText() != null){
            infoMapper.alterInfoData(info);
        }
        return infoMapper.alterInfoPara(info);
    }

    public int deleteInfo (int infoId){
        infoMapper.deleteInfoData(infoId);
        return infoMapper.deleteInfoPara(infoId);
    }

    public List<Info> getIndexInfo(){
        return infoMapper.getIndexInfo();
    }

    public List<Info> getSwiper(){
        return infoMapper.getSwiper();
    }

    public List<Info> searchInfoTitle(String infoTitle){
        return infoMapper.searchInfoTitle(infoTitle);
    }

    public Info getInfoDetail(Integer infoId){
        return infoMapper.getInfoDetail(infoId);
    }

    public List<Info> getListContinue(int type,String value,int pageStart, int pageSize){
        String typeStr = Integer.toString(type);
        return infoMapper.getListContinue(typeStr, value, pageStart, pageSize);
    }

    public List<Info> getInfoList(int type,String value,int pageStart, int pageSize){
        String typeStr = Integer.toString(type);
        return infoMapper.getList(typeStr, value, pageStart, pageSize);
    }

    public JSONObject list(String type, String value, String infoAuthName, String infoTitle, int pageNum, int pageSize) {
        int pageStart = (pageNum - 1) * pageSize; //加载起始
        JSONObject infoList = new JSONObject();
        List<Info> list = infoMapper.list(type, value, infoAuthName, infoTitle, pageStart, pageSize);
        int total = infoMapper.countInfo(type, value, infoAuthName, infoTitle);
        infoList.put("infoList", list);
        infoList.put("total", total);
        return infoList;
    }

    public int countInfo(String type, String value, String infoAuthName, String infoTitle) {
        return infoMapper.countInfo(type, value, infoAuthName, infoTitle);
    }

    public int countInfoUser() {
        return infoMapper.countInfoUser();
    }

    public int countSwiper(){
        return infoMapper.countSwiper();
    }

    public int countIndex(){
        return infoMapper.countIndex();
    }

    public List<Info> getMyInfo(String infoAuthor){
        return infoMapper.getMyInfo(infoAuthor);
    }

    public int alterInfoSwiper(int infoId, String infoSwiper){
        return infoMapper.alterInfoSwiper(infoId, infoSwiper);
    }

    public int alterInfoIndex(int infoId, String infoRank){
        return infoMapper.alterInfoIndex(infoId, infoRank);
    }
}
